/**
 ******************************************************************************
 * @file    lpit.c
 * @author  hyseim software Team
 * @date    25-Mar-2024
 * @brief   This file provides all the tmp functions.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Hyseim. Co., Ltd.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "lpit.h"

/** @addtogroup IM110GW_Driver
 * @{
 */

/** @defgroup LPIT
 * @brief LPIT driver modules
 * @{
 */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup LPIT_Private_Functions
  * @{
  */

/**
 * @brief  Initialize LPIT.
 * @param  LPIT_InitStruct: pointer to a LPIT_Init_t structure
 *         that contains the configuration information for the specified 
 *         LPIT peripheral.
 * @param  Channel: LPIT channel select.
 * @return None
 */
void LPIT_Init(LPIT_Init_t *LPIT_InitStruct, uint8_t Channel)
{   
    uint32_t mask = LPIT->MIER & ~(1U << Channel);

    LPIT->MIER = mask | (LPIT_InitStruct->LPIT_InterruptEnable << Channel);

    LPIT->TVAL[Channel].TVAL = LPIT_InitStruct->LPIT_TimerLoadValue;

    LPIT->TCTRL[Channel].TCTRL = LPIT_InitStruct->LPIT_TimerMode | LPIT_InitStruct->LPIT_ChainChannel |
                                 LPIT_InitStruct->LPIT_TSOT | LPIT_InitStruct->LPIT_TSOI |
                                 LPIT_InitStruct->LPIT_TROT | LPIT_InitStruct->LPIT_TriggerSelect;
}

/**
 * @brief  Fills each LPIT_StructInit member with its default value.
 * @param  LPIT_StructInit: pointer to a LPIT_Init_t structure which
 *         will be initialized.
 * @return None
 */
void LPIT_StructInit(LPIT_Init_t *LPIT_StructInit)
{
    LPIT_StructInit->LPIT_InterruptEnable = LPIT_Interrupt_Disable;
    LPIT_StructInit->LPIT_TimerLoadValue = 0xFFFFFFFF;
    LPIT_StructInit->LPIT_TimerMode = LPIT_Mode_Periodic_Counter;
    LPIT_StructInit->LPIT_ChainChannel = LPIT_Chain_Channel_Disable;
    LPIT_StructInit->LPIT_TSOT = LPIT_TSOT_Disable;
    LPIT_StructInit->LPIT_TSOI = LPIT_TSOI_Disable;
    LPIT_StructInit->LPIT_TROT = LPIT_TROT_Disable;
    LPIT_StructInit->LPIT_TriggerSelect = LPIT_Trigger_Select_Trgmux_In0;
}

/**
 * @brief  Starts the timer channel counting.
 * @param  Channel: LPIT channel select.
 * @return None
 */
void LPIT_StartTimerChannels(uint8_t Channel)
{
    LPIT->SETTEN |= (1U << Channel);
}

/**
 * @brief  Stops the timer channel from counting.
 * @param  Channel: LPIT channel select.
 * @return None
 */
void LPIT_StopTimerChannels(uint8_t Channel)
{
    LPIT->CLRTEN |= (1U << Channel);
}

/**
 * @brief  Sets the timer channel load value.
 * @param  TimerValue: Timer channel load value.
 * @param  Channel: LPIT channel select.
 * @return None
 */
void LPIT_SetTimerLoadValue(uint32_t TimerValue, uint8_t Channel)
{
    LPIT->TVAL[Channel].TVAL = TimerValue;
}

/**
 * @brief  Gets the timer channel load value.
 * @param  Channel: LPIT channel select.
 * @return Timer channel load value
 */
uint32_t LPIT_GetTimerLoadValue(uint8_t Channel)
{
    return LPIT->TVAL[Channel].TVAL;
}

/**
 * @brief  Gets the current timer channel counting value.
 * @param  Channel: LPIT channel select.
 * @return Current timer channel counting value
 */
uint32_t LPIT_GetCurrentTimerValue(uint8_t Channel)
{
    return LPIT->CVAL[Channel].CVAL;
}

/**
 * @brief  Enables the interrupt generation for timer channels.
 * @param  Channel: LPIT channel select.
 * @return None
 */
void LPIT_InterruptEnable(uint8_t Channel)
{
    LPIT->MIER |= (1U << Channel);
}

/**
 * @brief  Disables the interrupt generation for timer channels.
 * @param  Channel: LPIT channel select.
 * @return None
 */
void LPIT_InterruptDisable(uint8_t Channel)
{
    LPIT->MIER &= ~(1U << Channel);
}

/**
 * @brief  Gets the interrupt flag of timer channels.
 * @param  Channel: LPIT channel select.
 * @return The interrupt flag of timer channels
 */
FlagStatus_t LPIT_GetInterruptFlag(uint8_t Channel)
{
    ITStatus_t bitstatus = RESET;

    uint8_t flag = (LPIT->MSR & (1U << Channel)) >> Channel;

    if (flag != RESET)
    {
        bitstatus = SET;
    }
    else
    {
        bitstatus = RESET;
    }
    return bitstatus;
}

/**
 * @brief  Clears the interrupt flag of timer channels.
 * @param  Channel: LPIT channel select.
 * @return None
 */
void LPIT_ClearInterruptFlag(uint8_t Channel)
{
    LPIT->MSR = (1U << Channel);
}

/**
  * @}
  */

/**
 * @}
 */

/**
 * @}
 */
